package com.alu.sg.allstart.allstar.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.view.View;

import com.alu.sg.allstart.R;
import com.alu.sg.allstart.utils.DensityUtil;


/**
 * Created by Alu on 2017/9/14.
 * version 1.0
 * custom RecycleItemDecoration
 */

public class MyItemDecoration extends RecyclerView.ItemDecoration {

    private static final int INDEX = 0;
    private int wight;
    private int item_padding;
    private int dividerHeight;
    private Paint dividerPaint;
    private Paint topPaint;
    private Bitmap bitmap;
    private ObtainTextCallback callback;
    private TextPaint textPaint;
    private int text_padding;

    public MyItemDecoration(Context context, ObtainTextCallback obtainTextCallback) {
        this.callback = obtainTextCallback;
        wight = context.getResources().getDisplayMetrics().widthPixels;
//        height = context.getResources().getDisplayMetrics().heightPixels;
        //init paint
        dividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        dividerPaint.setColor(ContextCompat.getColor(context, R.color.color_blue_228bf7));
        dividerPaint.setAntiAlias(true);
        topPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        topPaint.setColor(ContextCompat.getColor(context, R.color.color_blue_228bf7));
        topPaint.setAntiAlias(true);
        textPaint = new TextPaint();
        textPaint.setColor(ContextCompat.getColor(context, R.color.white));
        textPaint.setAntiAlias(true);
        Rect text_rect = new Rect();
        //
        textPaint.getTextBounds("标题", 0, "标题".length(), text_rect);
        textPaint.setTextSize(DensityUtil.sp2px(context, 25));
        dividerHeight = DensityUtil.dp2px(context, 30); // 高度 1 dp
        item_padding = DensityUtil.dp2px(context, 10);//padding 10dp
        text_padding = DensityUtil.dp2px(context, 5);//padding 10dp
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher_round);
        ScaleBitmap();
    }

    //bitmap的大小和itemDecorationHeight进行比较对图片进行缩放操作(对性能有追求可以在加载到内存的时候进行压缩)
    private void ScaleBitmap() {
        Matrix matrix = new Matrix();
        float scale = bitmap.getWidth() > dividerHeight ? Float.valueOf(dividerHeight) / Float.valueOf(bitmap.getHeight()) : Float.valueOf(bitmap.getHeight()) / Float.valueOf(dividerHeight);
        matrix.postScale(scale, scale);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int childCount = parent.getChildCount();
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);
            float top = view.getTop() - dividerHeight;
            float bottom = top + dividerHeight;
            int position = parent.getChildAdapterPosition(view);
            String content = callback.getFirstText(position);
            if (isFirstInGroup(position)) {
                c.drawRect(left, top, right, bottom, dividerPaint);
                c.drawText(content, item_padding + bitmap.getWidth(), bottom - text_padding, textPaint);
                c.drawBitmap(bitmap, item_padding, bottom - bitmap.getHeight(), dividerPaint);
            }
        }

    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        View firstView = parent.getChildAt(INDEX);
        int position = parent.getChildAdapterPosition(firstView);
        String content = callback.getFirstText(position);
        if (firstView.getBottom() <= dividerHeight && isFirstInGroup(position + 1)) {
            c.drawRect(0, 0, wight, firstView.getBottom(), topPaint);
            c.drawText(content, item_padding + bitmap.getWidth(), firstView.getBottom() - text_padding, textPaint);
            c.drawBitmap(bitmap, item_padding, firstView.getBottom() - bitmap.getHeight(), dividerPaint);
        } else {
            c.drawRect(0, 0, wight, dividerHeight, topPaint);
            c.drawText(content, item_padding + bitmap.getWidth(), dividerHeight - text_padding, textPaint);
            c.drawBitmap(bitmap, item_padding, dividerHeight - bitmap.getHeight(), dividerPaint);
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        if (isFirstInGroup(position))
            outRect.top = dividerHeight;//类似加了一个bottom padding
    }

    /**
     * provide the first letter
     */
    public interface ObtainTextCallback {
        String getFirstText(int position);
    }

    //判断当前item和下一个item的第一个文字是否相同,如果相同说明是同一组,不需要画分割线
    private boolean isFirstInGroup(int pos) {
        if (pos == 0) {
            return true;
        } else {
            String prevGroupId = callback.getFirstText(pos - 1);
            String groupId = callback.getFirstText(pos);
            return !prevGroupId.equals(groupId);
        }
    }
}
